package cn.new7as.cas5s.ui.apply;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;

import cn.new7as.cas5s.ui.easy.netapi.PointApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityInfoFirstBinding;

import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.GetBaseDataApi;
import cn.new7as.cas5s.ui.easy.netapi.SaveBaseDataApi;


public class Info_FirstActivity extends BaseActivity {


    private ActivityInfoFirstBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInfoFirstBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Step1");
        show();
        getBase();
        initV();


    }

    @Override
    public void initV() {
        super.initV();

        setEtListener(binding.emailBa, binding.pincodeBa, binding.whatsappBa, binding.streetaddressBa, binding.towncityBa);
        setPoint(binding.whatsappBa, 1);
        setPoint(binding.emailBa, 2);
        setPoint(binding.towncityBa, 3);
        setPoint(binding.streetaddressBa, 4);
        setPoint(binding.pincodeBa, 5);


        binding.stateBa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showbottom();
            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String emailBa = binding.emailBa.getText().toString();
                String whatsappBa = binding.whatsappBa.getText().toString();
                String streetaddressBa = binding.streetaddressBa.getText().toString();
                String towncityBa = binding.towncityBa.getText().toString();
                String stateBa = binding.stateBa.getText().toString();
                String pincodeBa = binding.pincodeBa.getText().toString();

                JsonMap jsonMap = new JsonMap();
                JsonMap jsonMap2 = new JsonMap();
                jsonMap2.set("email", emailBa);
                jsonMap2.set("whatsApp", whatsappBa);
                jsonMap2.set("streetAddress", streetaddressBa);
                jsonMap2.set("state", stateBa);
                jsonMap2.set("pinCode", pincodeBa);
                jsonMap2.set("townCity", towncityBa);
                jsonMap.set("type", "1");
                jsonMap.set("personInfo", jsonMap2);
                HttpRequest.build(this, "http://16.163.9.142/cbf-app/message/smsSend")
                        .addHeaders("", "")
                        .setJsonParameter(jsonMap)
                        .setResponseListener(new ResponseListener() {
                            @Override
                            public void onResponse(String response, Exception error) {

                            }
                        })
                        .doPost();


                SaveBaseDataApi.Data data = new SaveBaseDataApi().new Data();
                data.type = 1;
                SaveBaseDataApi.Data1 data1 = new SaveBaseDataApi().new Data1();
                data1.email = emailBa;
                data1.whatsApp = whatsappBa;
                data1.streetAddress = streetaddressBa;
                data1.state = stateBa;
                data1.pinCode = pincodeBa;
                data1.townCity = towncityBa;
                data.personInfo = data1;
                show();
                EasyHttp.post(Info_FirstActivity.this)
                        .api(new SaveBaseDataApi(getunique(), null, 1, data1, null, null)).request(new OnHttpListener<HttpData<Object>>() {
                            @Override
                            public void onHttpSuccess(@NonNull HttpData<Object> result) {

                                point(getunique());


                            }

                            @Override
                            public void onHttpFail(@NonNull Throwable throwable) {
                                deal(throwable);

                            }
                        });
            }


        });
    }

    void getBase() {


        EasyHttp.post(Info_FirstActivity.this)
                .api(new GetBaseDataApi(getunique(), 1)).request(new OnHttpListener<HttpData<GetBaseDataApi.Data>>() {


                    @Override
                    public void onHttpSuccess(@NonNull HttpData<GetBaseDataApi.Data> result) {
                        hide();
                        if (result.getResult() != null) {
                            if (result.getResult().state == null || result.getResult().state.isEmpty()) {

                            } else sta = 1;
                            binding.stateBa.setText(result.getResult().state);
                            binding.emailBa.setText(result.getResult().email);
                            binding.whatsappBa.setText(result.getResult().whatsApp);
                            binding.streetaddressBa.setText(result.getResult().streetAddress);
                            binding.pincodeBa.setText(result.getResult().pinCode);
                            binding.towncityBa.setText(result.getResult().townCity);
                            setTrue();
                        }
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

    @Override
    public void setFalse() {
        super.setFalse();
        binding.verifyme.setEnabled(false);
    }

    int sta = -1;

    @Override
    public void setTrue() {
        super.setTrue();
        if (Utils.isStr(binding.emailBa, binding.pincodeBa, binding.streetaddressBa, binding.towncityBa, binding.whatsappBa) && Utils.isInt(sta))
            binding.verifyme.setEnabled(true);
    }

    String[] strings = new String[]{
            "Andhra Pradesh",
            "Arunachal Pradesh",
            "Assam",
            "Bihar",
            "Chandigarh",
            "Delhi",
            "Dadra and Nagar Haveli",
            "Daman and Diu",
            "Goa",
            "Gujarat",
            "Haryana",
            "Himachal Pradesh",
            "Jharkhand",
            "Karnataka",
            "Kerala",
            "Ladakh",
            "Lakshadweep",
            "Madhya Pradesh",
            "Maharashtra",
            "Manipur",
            "Meghalaya",
            "Mizoram",
            "Nagaland",
            "Odisha",
            "Puducherry",
            "Punjab",
            "Rajasthan",
            "Sikkim",
            "Tamil Nadu",
            "Telangana",
            "Tripura",
            "Uttar Pradesh",
            "Uttarakhand",
            "West Bengal"
    };

    long editstart;
    int whatscount;
    int whatsDuration;
    int mailcount;
    int mailDuration;
    int towncount;
    int townDuration;
    int addresscount;
    int addressDuration;
    int statecount;
    int astateDuration;
    int pincount;
    int pinDuration;

    void setPoint(EditText editText, int type) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    switch (type) {
                        case 1:
                            whatscount++;
                            break;
                        case 2:
                            mailcount++;
                            break;
                        case 3:
                            towncount++;
                            break;
                        case 4:
                            addresscount++;
                            break;
                        case 5:
                            pincount++;
                            break;
                    }

                    editstart = System.currentTimeMillis();
                } else {
                    int i = (int) ((System.currentTimeMillis() - editstart) / 1000);
                    Log.e("dura", i + "");
                    switch (type) {
                        case 1:
                            whatsDuration += i;
                            break;
                        case 2:
                            mailDuration += i;
                            break;
                        case 3:
                            townDuration += i;
                            break;
                        case 4:
                            addressDuration += i;
                            break;
                        case 5:
                            pinDuration += i;
                            break;
                    }

                }
            }
        });
    }

    private int ba;

    @Override
    protected void onStop() {
        super.onStop();
        ba++;
    }

    private void point(String s) {
        long end = System.currentTimeMillis();
        PointApi.Point2Data pointData = new PointApi().new Point2Data();
        pointData.type = 1;
        pointData.address_duration = addressDuration;
        pointData.address_updatecount = addresscount;
        pointData.city_duration = townDuration;
        pointData.city_updatecount = towncount;
        pointData.email_updatecount = mailcount;
        pointData.email_duration = mailDuration;
        pointData.whatsapp_duration = whatscount;
        pointData.whatsapp_updatecount = whatsDuration;

        pointData.state_updatecount = statecount;
        pointData.state_duration = astateDuration;
        pointData.pincode_updatecount = pincount;
        pointData.pincode_duration = pinDuration;


        pointData.basicpage_backend_num = ba;

        pointData.basicpage_stop_duration = (int) ((end - start) / 1000);
        pointData.frist_click_time = Utils.getdate(start);
        pointData.end_click_time = Utils.getdate(end);
        EasyHttp.post(this)
                .api(new PointApi(s, pointData)).request(new OnHttpListener<HttpData<Object>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<Object> result) {
                        Utils.go(Info_FirstActivity.this, Info_SecondActivity.class);
                        finish();
                        hide();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });

    }


    void showbottom() {
        statecount++;
        editstart = System.currentTimeMillis();
        BasePopupView state = new XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .maxHeight(900)
                .asBottomList("State", strings,
                        null, -1,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                binding.stateBa.setText(text);
                                sta = position;
                            }
                        })

                .show();
        state.getLifecycle().addObserver(new LifecycleObserver() {
            @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
            public void ondismiss() {
                astateDuration += (int) ((System.currentTimeMillis() - editstart) / 1000);

            }
        });

    }

}